package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.*;
import com.itheima.health.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServicelmpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private OrderDao orderDao;


    public MemberReportVO getMemberReport(LocalDate begin, LocalDate end) {
        //日期区间
        //创建一个ListDate集合 封装循环遍历的日期
        List<LocalDate> dateList = new ArrayList<>();
        //设置while循环 begin-end
        while (begin.equals(end.plusDays(1))){
            dateList.add(begin);
            begin = begin.plusDays(1);
        }

        //指定日期的会员数据
        //创建一个会员集合
        List<Integer> memberList = new ArrayList<>();

        //获取某一天的开始时间到结束时间
        //LocalDateTime.of 将LocalDate转换成LocalDateTime
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime   = LocalDateTime.of(date, LocalTime.MAX);


            //将查询条件封装到Map集合
            Map map = new HashMap();
            map.put("end",endTime);

            Integer member = memberDao.countMemberByMap(map);
            member = member == null ? 0 : member;
            memberList.add(member);

        }
        List<String> months = new ArrayList<>();
        months.add("2023.01");
        months.add("2023.02");
        months.add("2023.03");
        months.add("2023.04");
        months.add("2023.05");

        return MemberReportVO
                .builder()
                .months(months)
                .memberCount(memberList)
                .build();
    }

    /**
     * 套餐预约占比饼形图
     * @return
     */
    public SetmealReportVO getSetmealReport() {
        //将查询的所有检查项封装到套餐集合中
        List<Setmeal> setmeals = setmealDao.selectAll();

        //创建一个套餐名字集合,封装所有套餐名字
        List<String> setmealNames = new ArrayList<>();

        List<SetmealCount> setmealCount = new ArrayList<>();
        //循环遍历套餐
        for (Setmeal setmeal : setmeals) {

            //每遍历一次 就获取一次套餐名字进行封装
            setmealNames.add(setmeal.getName());
            //每遍历一次 根据套餐id获取订单 进行订单统计
            Integer integer = orderDao.countBySetmealId(setmeal.getId());

            //创建一个套餐项对象
            SetmealCount setmealCountItem = new SetmealCount();
            //根据套餐获取名字 value
            setmealCountItem.setName(setmeal.getName());
            setmealCountItem.setValue(integer);

            //将套餐项封装到setmealCount集合
            setmealCount.add(setmealCountItem);
        }
        return SetmealReportVO
                .builder()
                .setmealNames(setmealNames)
                .setmealCount(setmealCount)
                .build();
    }
}
